"""
Case Type   : resource pool
Case Name   : 多租户场景mem_percent默认值20，相加不大于100，部分测试点合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建组资源池和业务资源池，不指定mem_percent,查询
    3.创建业务资源池指定workload_a2控制组，mem_percent=81
    4.创建业务资源池指定workload_a2控制组，mem_percent=10
    5.修改step4创建的业务资源池mem_percent=85
    6.创建组资源池指定class_b控制组，mem_percent=90，相加大于100，应创建失败
    7.创建组资源池指定class_b控制组，mem_percent=50
    8.修改step7创建的组务资源池mem_percent=81
    9.环境清理
Expect      :
    1.创建成功
    2.创建成功，默认mem_percent=20
    3.相加大于100，应创建失败合理报错
    4.创建成功
    5.相加大于100，修改失败，合理报错
    6.合理报错
    7.创建成功
    8.mem_percent相加=101，合理报错
    9.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_a = 'class_a'
        self.class_b = 'class_b'
        self.group_a1 = 'workload_a1'
        self.group_a2 = 'workload_a2'
        self.respool = [f'resp_resource_pool_0052_{i}' for i in range(1, 5)]

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_a} -s 20; " \
              f"gs_cgroup -c -S {self.class_a} -G {self.group_a1} -g 20; " \
              f"gs_cgroup -c -S {self.class_a} -G {self.group_a2} -g 10; " \
              f"gs_cgroup -c -S {self.class_b} -s 30; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_b}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_a1}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_a2}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建组资源池和业务资源池，不指定mem_percent,查询;" \
               "expect:创建成功，默认mem_percent=20-----"
        self.log.info(text)
        sql = f'''create resource pool {self.respool[0]} 
        with(control_group = '{self.class_a}');
        create resource pool {self.respool[1]} 
        with(control_group = '{self.class_a}:{self.group_a1}');
        select pg_sleep(3);
        select respool_name, mem_percent, control_group from pg_resource_pool 
        where respool_name in('{self.respool[0]}','{self.respool[1]}');'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            self.constant.create_resource_pool_success), f'执行失败:{text}')
        self.assertIn(f'{self.respool[0]} |          20 | {self.class_a}\n '
                      f'{self.respool[1]} |          20 | {self.class_a}:'
                      f'{self.group_a1}', sql_res, f'执行失败:{text}')

        text = "-----step3:创建业务资源池指定workload_a2控制组，mem_percent=81;" \
               "expect:相加大于100，应创建失败，合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[2]} with("
            f"control_group='{self.class_a}:{self.group_a2}',mem_percent=81);")
        self.log.info(sql_res)
        flag = 'ERROR:  memory percent value is beyond the available range.'
        self.assertIn(flag, sql_res, f'执行失败:{text}')

        text = "-----step4:创建业务资源池指定workload_a2控制组，mem_percent=10;" \
               "expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[2]} with("
            f"control_group='{self.class_a}:{self.group_a2}',mem_percent=10);")
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')

        text = "-----step5:修改step4创建的业务资源池mem_percent=85;" \
               "expect:相加大于100，修改失败，合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter resource pool {self.respool[2]} with(mem_percent = 85);")
        self.log.info(sql_res)
        self.assertIn(flag, sql_res, f'执行失败:{text}')

        text = "-----step6:创建组资源池指定class_b控制组，mem_percent=90，" \
               "相加大于100，应创建失败;expect:合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[3]} "
            f"with(control_group = '{self.class_b}', mem_percent = 90);")
        self.log.info(sql_res)
        self.assertIn(flag, sql_res, f'执行失败:{text}')

        text = "-----step7:创建组资源池指定class_b控制组，mem_percent=50;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[3]} "
            f"with(control_group = '{self.class_b}', mem_percent = 50);")
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')

        text = "-----step8:修改step7创建的组务资源池mem_percent=81;" \
               "expect:mem_percent相加=101，合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter resource pool {self.respool[3]} with(mem_percent = 81);")
        self.log.info(sql_res)
        self.assertIn(flag, sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step9:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            ';\n'.join(
                [f"drop resource pool if exists {i}" for i in self.respool]))
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(4, drop_res.count(
            self.constant.drop_resource_pool_success), f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
